<?php defined('BASEPATH') or exit('No direct script access allowed');

class Infostreams_m extends MY_Model
{
    
    public $infostream;
    public $infostream_fields;

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('cookie');
        $this->load->helper('date');

        $this->load->driver('Streams');

        // Get the user ID, if it exists
        if($this->ion_auth->logged_in())
        {
            $this->user_id = $this->current_user->id;
        }

        $this->infostream = $this->streams_m->get_stream('infostream', true, 'streams');
        $this->infostream_fields = $this->streams_m->get_stream_fields($this->
            infostream->id);
    }

    function getInfoStreams($where)
    {

        if (!is_array($where)) {
            $where = array('id' => $where);
        }

        return $this->db->select('*')->where($where)->order_by('created_on', 'ASC')->
            get('InfoStreams')->result();

    }

    function getPropertyValues($where)
    {


        if (!is_array($where)) {
            $where = array('id' => $where);
        }

        $InfoStreams = $this->db->select('  InfoStreams.id          AS stream_id, 
                        InfoStreams.source_id   AS source_id, 
                        InfoStreams.identifier  AS identifier, 
                        InfoStreams.created_on  AS created_on, 
                        InfoStreams.title       AS title, 
                        InfoStreams.status      AS status, 
                        PropertyValues.id          AS value_id,
                        PropertyValues.property_id AS property_id,
                        PropertyValues.value       AS value')
                        ->join('ISPropertyValues AS PropertyValues', 'PropertyValues.object_id = InfoStreams.id', 'left')
                        ->join('ISPropertiesObjects AS PropertiesObjects', 'PropertiesObjects.id = PropertyValues.property_id', 'left')
                        ->where($where)
                        ->order_by('InfoStreams.created_on', 'ASC')
                        ->order_by('PropertiesObjects.sort', 'ASC')
                        ->order_by('PropertyValues.property_id', 'ASC')
            ->get('InfoStreams')->result();

        $PropertiesObjects = $this->db->select('*')->order_by('sort', 'ASC')->get('ISPropertiesObjects')->
            result();


        $InfoStreamsObjects = array();

        foreach ($InfoStreams as $row):

            $InfoStreamsObjects[$row->stream_id][] = $row;

        endforeach;

        unset($InfoStreams);

        $stStreams = array();

        foreach ($InfoStreamsObjects as $stream_id => $InfoStreamsRows):

            $curStreams = &$stStreams;

            $i = 0;

            foreach ($PropertiesObjects as $Prop):

                foreach ($InfoStreamsRows as $row):

                    if ($row->property_id == $Prop->id):

                        $color = null;

                        if (isset($curStreams[$row->value]) == false):

                            $curStreams[$row->value] = array(
                                'value' => $row->value,
                                'name' => $Prop->name,
                                'color' => $color,
                                'level' => $i,
                                'childs' => array());

                        endif;

                        if ($row->status == 'close') {
                            $curStreams[$row->value]['color'] = "#ccc";
                        } elseif ($row->status == 'update') {
                            $curStreams[$row->value]['color'] = "#f9f0ab";
                        } else {
                            $curStreams[$row->value]['color'] = "#0099FF";
                        }


                        $curStProperty = &$curStreams[$row->value];
                        $curStreams = &$curStProperty['childs'];

                    endif;

                endforeach;

                $i++;

            endforeach;

        endforeach;

        unset($InfoStreamsObjects);
        unset($PropertiesObjects);

        return $stStreams;

    }

    public function get_InfostreemTree($parent_id = 0)
    {
        $this->db->where('created_by', $this->user_id);
        $data = $this->streams_m->get_stream_data($this->infostream, $this->
            infostream_fields);
           
        $level = 0;
            
        $parentdata = array();
        $cur_row = false;
        if($data):
            foreach($data as $row):
                $parentdata[$row->parent][] = $row;
                if($row->id == $parent_id):
                    //$cur_row = $row;  
                    $level = $row->level + 1;   
                endif;
            endforeach;
        endif;
         
         /*       
        if($cur_row !== false){
            $level = $cur_row->level++;
        }else{
            $level = 0;    
        }
        */

        return $this->build_tree($parentdata, $parent_id, $level);

    }

    private function build_tree($data, $parent_id, $level, $i =0)
    {   
     
       $tree = array();
       if($i > 1000) return $tree;
       if (is_array($data) and (isset($data[$parent_id])) and count($data[$parent_id]) > 0):
            foreach ($data[$parent_id] as $row):
                if($row->level == $level):
                    $tree[$row->id] = $row; 
                    $tree[$row->id]->_is_last_level = (isset($data[$row->id]) ? false : true); $i++;
                    $tree[$row->id]->_childs = $this->build_tree($data, $row->id, $level, $i);

                    $is_last_level = true;
                    if(isset($data[$row->id])){
                        foreach ($data[$row->id] as $key => $value) {
                            if($value->level > $row->level){
                                $is_last_level = false;
                                break;
                            }
                        }
                    }
                    $tree[$row->id]->_is_last_level = $is_last_level;
                endif;
            endforeach;
        endif;
        
        return $tree;
    }


    /*
    $result=mysql_query("SELECT id, parent_id, title FROM tree");
    $cats = array();
    while($cat =  mysql_fetch_assoc($result))
    $cats[$cat['parent_id']][] =  $cat;
    
    function  build_tree($cats,$parent_id){
    if(is_array($cats) and count($cats[$parent_id])>0){
    $tree = '<ul>';
    foreach($cats[$parent_id] as $cat){
    $tree .= '<li>'.$cat['title'];
    $tree .=  build_tree($cats,$cat['id']);
    $tree .= '</li>';         
    }
    $tree .= '</ul>';
    } 
    else return null;          
    return $tree; 
    }
    echo build_tree($cats,0); // :)))   
    */
    
    public function Add($post)
    {
        if ( ! $result_id = $this->row_m->insert_entry($post, $this->infostream_fields, $this->infostream, array()))
		{
		  return 0;
        } else {
            
            return $result_id;
        }
			
    }
    
    public function getLevelParent($parent_id)
    {
        //$this->db->where('id', $parent_id);
        $data = $this->streams_m->get_stream_data($this->infostream, $this->
            infostream_fields);
        
        $level = 0;
        if(sizeof($data) > 0):
        //    $level = $data[0]->level;
        //endif;
        //if(is_array($data)):
            foreach($data as $row):
                if($row->id == $parent_id):
                    $level = $row->level;    
                    break;
                endif;
            endforeach;
        endif;

        return $level;         
        
    }

    public function check_version($id, $version)
    {
        $this->db->where('id', $id);
        $data = $this->streams_m->get_stream_data($this->infostream, $this->
            infostream_fields);
        
        $version = $version + 0;

        $check_version = false;
        if(sizeof($data) > 0):
            foreach($data as $row):
                if($version == $row->version):
                    $check_version = true;
                    break;
                endif;
            endforeach;
        endif;
        
        return $check_version;         
        
    }
    
    public function delete($id)
    {
        return $this->row_m->delete_row($id, $this->infostream);   
    }
    
    public function update($id, $post)
    {
		$skips = array();
		foreach ($this->infostream_fields as $field)
		{
			if(!isset($post[$field->field_slug])){
				$skips[] = $field->field_slug;	
			}
		}
		
        return $this->row_m->update_entry($this->infostream_fields, $this->infostream, $id, $post, $skips);
    }
}

?>